package com.cfets.annualAssessment.service;

import com.cfets.annualAssessment.entity.Attribute;
import com.cfets.annualAssessment.entity.Customer;
import com.cfets.annualAssessment.mapper.CustomerMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Created by liusonglin on 2016/11/21.
 */
@Service
public class CustomerService {
    @Autowired
    private CustomerMapper customerMapper;

    public List<Customer> getCustomerPage(Customer customer) {
        Page<?> page = PageHelper.startPage(customer.getPage(), customer.getRows());
        List<Customer> customerList=null;
        Example example=new Example(Customer.class);
        Example.Criteria criteria = example.createCriteria();
//        example.setOrderByClause("post_time DESC");
        if(customer.getGender()==1||customer.getGender()==2){
            criteria.andEqualTo("gender",customer.getGender());
        }
        if(!StringUtils.isEmpty(customer.getName())){
            criteria.andLike("name","%"+customer.getName()+"%");
        }
        customerList = customerMapper.selectByExample(example);
        customer.setTotalCount(page.getTotal());
        return customerList;
    }

    public boolean newAttribute(Customer customer) {
        return customerMapper.insertSelective(customer)>0;
    }

    public boolean updateAttribute(Customer customer) {
        return customerMapper.updateByPrimaryKey(customer)>0;
    }

    public boolean deleteAttributeById(int id) {
        return customerMapper.deleteByPrimaryKey(id)>0;
    }
}
